<?php
/*
	插件概览
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
set_html('title','登录商户');
if($_RQ['op']=='get'){
	$_SESSION['logout_forward']='jy_merchscrm/login/';
	$_SESSION['forward_url']='jy_merchscrm/merch.index/'; 
	$plugin_name='jy_merchscrm';
	$pid=pdo_getcolumn('core_plugins',array('name'=>$plugin_name),'id');
	if($_SESSION['os']=='windows'){
		$item=pdo_get('core_open_plugin',array('pid'=>$pid,'opentype'=>'wxwork','uniacid'=>$_SESSION['uniacid'],'isplugin'=>1));
		$backurl=urlencode(custom_url('jy_merchscrm/login/onelogin/',true));
		$state=session_id();
		$return['wwopen']='https://login.work.weixin.qq.com/wwlogin/sso/login?appid='.$item['suite_id'].'&redirect_uri='.$backurl.'&state='.$state.'&login_type=ServiceApp';
		$return['item']=$item;
		$return['info']=array();
		$return['login_type']=0;
		exi($return);
	}else{
		$item=pdo_get('core_open_plugin',array('pid'=>$pid,'opentype'=>'wxwork','uniacid'=>$_SESSION['uniacid'],'isplugin'=>1));
		$backurl=urlencode(custom_url('jy_merchscrm/login/onelogin/',true));
		$state=session_id();
		$item['auth_url']='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$item['suite_id'].'&redirect_uri='.$backurl.'&response_type=code&scope=snsapi_base&agentid='.$item['suite_id'].'&state='.$state.'#wechat_redirect';
		$code=$_RQ['code'];
		if($code){
			cfc('workfans_api')->upfans($item['id'],$code);
		}else{
			if(!$_SESSION['open_userid']){
				exi('','',$item['auth_url']);
			}
		}
		if(!$_SESSION['open_userid'] || !$_SESSION['uid'] || $_SESSION['open_userid']=='isget'){
			$return['item']=$item;
			exi($return);
		}else{
			$url=custom_url($plugin_name.'/merch.index/',true);
			exi('','',$url);
		}
	}
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	if(!$post['password'] || !$post['username']){
		exi('请填写账户名和密码','error');
	}
	$user=pdo_get('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'uname'=>$post['username']));
	if(!$user || $post['password']!=base64_decode($user['upass'])){
		exi('用户名或密码错误','error');
	}
	if(!$user['mid']){
		exi('该商户没有绑定用户/管理员！','error');
	}
	$_SESSION['uid']=$user['mid'];
	$_SESSION['merchid']=$_SESSION['onemerchid']=$user['id'];
	$_SESSION['member']['username']=$user['uname'];
	unset($_SESSION['workertype']);
	//绝对地址，会刷新页面
	if(!$_SESSION['wxapp_id']){
		$url=custom_url('jy_merchscrm/merch.index/',true);
	}else{
		$url='jy_merchscrm/merch.index/';
	}
	exi('登陆成功!','',$url);
}
if($_RQ['op']=='onelogin'){
	$plugin_name='jy_merchscrm';
	$pid=pdo_getcolumn('core_plugins',array('name'=>$plugin_name),'id');
	$item=pdo_get('core_open_plugin',array('pid'=>$pid,'opentype'=>'wxwork','uniacid'=>$_SESSION['uniacid'],'isplugin'=>1));
	$code=$_RQ['code'];
	$uid=cfc('workfans_api')->upfans($item['id'],$code);
	$merchid=model('jy_merch/model')->checkadd_merch('',1);
	$user=pdo_get('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$merchid));
	if(!$user['mid']){
		exi('授权失败!','error','/login/');
	}
	$_SESSION['uid']=$user['mid'];
	$_SESSION['merchid']=$user['id'];
	$_SESSION['member']['username']=$user['uname'];
	$url=custom_url('jy_merchscrm/merch.index/',true);
	exi('','',$url);
}